package org.grottarossa.ui.core.Dialog;


import java.util.ResourceBundle;

import org.grottarossa.ui.core.i18n.DM;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

@SuppressWarnings("deprecation")
public class MessageBox implements ClickListener {

	/**
	 *
	 */
	private static final long serialVersionUID = 4635095197524580585L;

	ResourceBundle localizer = DM.instance().getBundle(getClass().getName());

	Window window = null;
	String title = "";
	String message = "";
	Buttons buttons;
	MessageBoxListener listener;
	Object object;

	public ButtonType Response;



	public enum Buttons
	{
		OK(0),
		YesNo(1);

		private int val;

		Buttons(int val)
		{
			this.setVal(val);
		}

		public void setVal(int val) {
			this.val = val;
		}

		public int getVal() {
			return val;
		}
	}

	public enum ButtonType {
		OK(0),
		Yes(1),
		No(2);

		private int val;

		ButtonType(int val)
		{
			this.setVal(val);
		}

		public void setVal(int val) {
			this.val = val;
		}

		public int getVal() {
			return val;
		}
	}

	private void init(Window window2) {

		window = new Window(title);
		window.setModal(true);

		VerticalLayout layout = (VerticalLayout) window.getContent();
        layout.setMargin(true);
        layout.setSpacing(true);
        layout.setSizeUndefined();

        Label lblCaption = new Label();
		lblCaption.setCaption(this.message);
		lblCaption.setContentMode(Label.CONTENT_TEXT);
		layout.addComponent(lblCaption);

        HorizontalLayout hLayout = new HorizontalLayout();
        hLayout.setMargin(true);
        hLayout.setSpacing(true);

        if ( buttons == Buttons.OK ) {

        	Button ok = addButton(ButtonType.OK);
        	layout.addComponent(ok);
        	layout.setComponentAlignment(ok, Alignment.MIDDLE_CENTER);

        } else if ( buttons == Buttons.YesNo ) {

        	Button yes = addButton(ButtonType.Yes);
        	hLayout.addComponent(yes);
        	hLayout.setComponentAlignment(yes, Alignment.MIDDLE_CENTER);

        	Button no = addButton(ButtonType.No);
        	hLayout.addComponent(no);
        	hLayout.setComponentAlignment(no, Alignment.MIDDLE_CENTER);
        }

        layout.addComponent(hLayout);

        window2.addWindow(window);
	}

	public MessageBox(Window window2, String title, String message, Buttons buttons, MessageBoxListener listener, Object object)
	{
		this.title = title;
		this.message = message;
		this.buttons = buttons;
		this.listener = listener;
		this.object = object;
		this.init(window2);
	}

	private Button addButton (ButtonType btnType)
	{
		String sTitle = "";

		if ( btnType == ButtonType.OK )
			sTitle = localizer.getString("button.title.OK");

		else  if ( btnType == ButtonType.No )
			sTitle = localizer.getString("button.title.No");

		else  if ( btnType == ButtonType.Yes )
			sTitle = localizer.getString("button.title.Yes");

		Button btn = new Button(sTitle, this);
		btn.setData(btnType);

		return btn;
	}

	public void buttonClick(ClickEvent event)
	{
		((Window) window.getParent()).removeWindow(window);
		listener.manageButton((ButtonType)event.getButton().getData(), object);
	}
}
